<?php

// $Id$

class ContextKnowledge {

  function __construct($item = NULL) {
    module_load_include('inc', 'fedora_repository', 'api/fedora_item');
    if (!empty($item)) {
      $this->owner = $item;
      if ( array_key_exists('RDF-CONTEXT', $item->get_datastreams_list_as_array())) {
        $meta = $item->get_datastream_dissemination('RDF-CONTEXT');
        if (!empty($meta)) {
          $this->metaDataXML = $meta;
        }
      }
    }
  }

  public function buildDrupalForm($form = array()) {
        
    $meta_xml = $this->metaDataXML;
        
    $meta = DOMDocument::loadXML($meta_xml);
	
	$form['event:title'] = array(
	  '#title' => 'event:title',
	  '#type' => 'textfield',
	  '#default_value' => $meta->getElementsByTagNameNS('http://www.openarchive.org/event/', 'title')->item(0)->nodeValue,
	  '#description' => '',
	);
	
	$form['event:place'] = array(
	  '#title' => 'event:place',
	  '#type' => 'textfield',
	  '#default_value' => $meta->getElementsByTagNameNS('http://www.openarchive.org/event/', 'place')->item(0)->nodeValue,
	  '#description' => '',
	);
	
	$form['event:time'] = array(
	  '#title' => 'event:time',
	  '#type' => 'textfield',
	  '#default_value' => $meta->getElementsByTagNameNS('http://www.openarchive.org/event/', 'time')->item(0)->nodeValue,
	  '#description' => '',
	);
	
	$form['event:agent'] = array(
	  '#title' => 'event:agent',
	  '#type' => 'textfield',
	  '#default_value' => $meta->getElementsByTagNameNS('http://www.openarchive.org/event/', 'agent')->item(0)->nodeValue,
	  '#description' => '',
	);
	
	$form['event:product'] = array(
	  '#title' => 'event:product',
	  '#type' => 'textfield',
	  '#default_value' => $meta->getElementsByTagNameNS('http://www.openarchive.org/event/', 'product')->item(0)->nodeValue,
	  '#description' => '',
	);

	/************ Example ************
	/*
		$form['dc:type'] = array(
		  '#title' => 'dc:type',
		  '#type' => 'textfield',
		  '#default_value' => $meta->getElementsByTagNameNS('http://purl.org/dc/terms/', 'type')->item(0)->nodeValue,
		  '#description' => '',
		);
		
		 $form['dwc:basisOfRecord'] = array(
		  '#title' => 'Basis of Record',
		  '#type' => 'select',
		  '#default_value' => $dwc->getElementsByTagNameNS('http://rs.tdwg.org/dwc/terms/', 'basisOfRecord')->item(0)->nodeValue,
		  '#options' => $this->vocabulary['basisOfRecord'],
		  '#description' => '',
		);
	*/

    
    return $form;
  }

  public function handleForm($form_values) {
    module_load_include('inc', 'fedora_repository', 'api/fedora_item');
    global $user;
    //$new_item = Fedora_Item::ingest_new_item($form_values['pid'], 'A', $form_values['dwc:institutionCode'].':'.$form_values['dwc:collectionCode'].':'.$form_values['dwc:catalogNumber'], $user->name);
    //$new_item->add_datastream_from_string($this->metaDataXML, 'RDF-CONTEXT', 'Knowledge Reference Metadata', 'text/xml', 'X');
    // For each form element, find and replace that element's value in the XML stream.
    $meta = new SimpleXMLElement($this->metaDataXML);
	
	$meta_description = $meta->children('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
	$meta_description->Description->attributes('http://www.w3.org/1999/02/22-rdf-syntax-ns#')->about = "info:fedora/" . $form_values['pid'];
	$meta_elements = $meta_description->Description->children('http://www.openarchive.org/event/');
	$meta_elements->title = $form_values['event:title'];
	$meta_elements->place = $form_values['event:place'];
	$meta_elements->time = $form_values['event:time'];
	$meta_elements->agent = $form_values['event:agent'];
	$meta_elements->product = $form_values['event:product'];
	
	//************ Example ************
	
	/*
		$dc_elements = $meta->SimpleDarwinRecord->children('http://purl.org/dc/terms/');
		$dc_elements->type = $form_values['dc:type'];
		
		$meta_elements = $meta->SimpleDarwinRecord->children('http://rs.tdwg.org/dwc/terms/');
		$meta_elements->basisOfRecord = $form_values['dwc:basisOfRecord'];
    */

    $this->metaDataXML = $meta->saveXML();
  }

  public function asXML() {
    return $this->metaDataXML;
  }

  public function asHTML() {
    $path=drupal_get_path('module', 'Fedora_Repository');
    module_load_include('inc', 'fedora_repository', 'ObjectHelper');
    module_load_include('inc', 'fedora_repository', 'CollectionClass');

    $xmlstr = $this->metaDataXML;
    html_entity_decode($xmlstr);

    if ($xmlstr == NULL || strlen($xmlstr) < 5) {
      return " ";
    }

    try {
      $proc = new XsltProcessor();
    }
    catch (Exception $e) {
      drupal_set_message(t("!e", array('!e' => $e->getMessage())), 'error');
      return " ";
    }

    $xsl = new DomDocument();
    $xsl->load($path . '/xsl/contextview.xsl');
    $input = new DomDocument();
    $input->loadXML(trim($xmlstr));
    $xsl = $proc->importStylesheet($xsl);
    $newdom = $proc->transformToDoc($input);
    $content=$newdom->saveXML();

    return $content;
  }

  public $vocabulary = array(
    'basisOfRecord' => array(
      'Occurrence' => 'Occurrence',
      'Event' => 'Event',
      'Location' => 'Location',
      'Taxon' => 'Taxon',
      'PreservedSpecimen' => 'PreservedSpecimen',
      'FossilSpecimen' => 'FossilSpecimen',
      'LivingSpecimen' => 'LivingSpecimen',
      'HumanObservation' => 'HumanObservation',
      'MachineObservation' => 'MachineObservation',
      'NomenclaturalChecklist' => 'NomenclaturalChecklist',
    ),
    
  );

  public $metaFields = array(
  	'event:title',
	'event:place',
	'event:time',
	'event:agent',
	'event:product'
	
  	//********** Example ***********
	/*
		'dc:type',
		'dwc:basisofrecord',
	*/
   
  );


  public $metaDataXML = '
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:event="http://www.openarchive.org/event/">

  <rdf:Description rdf:about="">
    <event:title></event:title>
    <event:place></event:place>
    <event:time></event:time>
    <event:agent></event:agent>
    <event:product></event:product>
  </rdf:Description>
</rdf:RDF>';

}
